package org.jash.shop;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.jash.shop.entity.CartEntity;

import java.util.List;

public class CartAdapter extends BaseAdapter {
    private final List<CartEntity.DataDTO> data;
    private Callback callback;
    public CartAdapter(List<CartEntity.DataDTO> data) {
        this.data = data;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public CartEntity.DataDTO getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        CartEntity.DataDTO dto = data.get(position);
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.title.setText(dto.getGoodsDesc());
        holder.price.setText("￥" + dto.getGoodsPrice());
        holder.count.setText("" + dto.getCarNum());
        Glide.with(holder.icon)
                .load(dto.getGoodsIcon())
                .into(holder.icon);
        //选中事件
        holder.checked.setOnCheckedChangeListener((buttonView, isChecked) -> {
            dto.setChecked(isChecked);
            if (callback != null) {
                callback.update();
            }
        });
        //增加数量
        holder.plus.setOnClickListener(v -> {
            dto.setCarNum(dto.getCarNum() + 1);
            if (callback != null) {
                callback.update();
            }
            notifyDataSetChanged();
        });
        // 减少数量
        holder.minus.setOnClickListener(v -> {
            if (dto.getCarNum() == 1) {
                Toast.makeText(v.getContext(), "不能再减了", Toast.LENGTH_SHORT).show();
                return;
            }
            dto.setCarNum(dto.getCarNum() - 1);
            if (callback != null) {
                callback.update();
            }
            notifyDataSetChanged();
        });
        holder.checked.setChecked(dto.isChecked());
        return convertView;
    }
    public static class ViewHolder {
        private final CheckBox checked;
        private final TextView title, price, minus, plus, count;
        private final ImageView icon;

        public ViewHolder(View itemView) {
            checked = itemView.findViewById(R.id.checked);
            title = itemView.findViewById(R.id.title);
            price = itemView.findViewById(R.id.price);
            minus = itemView.findViewById(R.id.minus);
            plus = itemView.findViewById(R.id.plus);
            count = itemView.findViewById(R.id.count);
            icon = itemView.findViewById(R.id.icon);
        }
    }
    public interface Callback {
        void update();
    }
}
